package com.pjb.springbootjjwt.store;

import com.pjb.springbootjjwt.store.storage.DDL;
import com.pjb.springbootjjwt.store.storage.DML;

public class Main {

    private static final DDL ddl = new DDL();
    private static final DML dml = new DML();

    public static String cmd(String cmd) throws Exception {
        String r = "success";
        cmd = cmd.toLowerCase();

        //ddl
        if (cmd.startsWith("show")) {
            ddl.showTables();
        }
        if (cmd.startsWith("create")) {
            String[] s = cmd.split("\\s+");
            ddl.create(s[2], cmd);
        }
        if (cmd.startsWith("desc")) {
            String[] s = cmd.split("\\s+");
            ddl.desc(s[2]);
        }
        if (cmd.startsWith("drop")) {
            String[] s = cmd.split("\\s+");
            ddl.drop(s[2], s[1]);
        }
        if (cmd.startsWith("use")) {
            String[] s = cmd.split("\\s+");
            ddl.use(s[1]);
        }

        //dml
        if (cmd.startsWith("insert")) {
            String sub = cmd.substring(cmd.indexOf("into"), cmd.indexOf("("))
                    .replaceAll("into", "").trim();
            dml.insert(sub, cmd);
        }
        if (cmd.startsWith("delete")) {
            String[] s = cmd.split("\\s+");
            dml.delete(s[2], cmd);
        }
        if (cmd.startsWith("update")) {
            String[] s = cmd.split("\\s+");
            dml.update(s[2], cmd);
        }
        if (cmd.startsWith("select")) {
            String[] s = cmd.split("\\s+");
            r = dml.select(cmd).toString();
        }
        return r;
    }

}
